package web.servlet;

import commons.Constatns;
import pojo.User;
import service.DeletUserService;
import service.Impl.DeletUserServiceImpl;
import service.Impl.UpdateUserServiceImpl;
import service.Impl.UserLoginServiceImpl;
import service.Impl.UserSelectServiceImpl;
import service.UpdateUserService;
import service.UserLoginService;
import service.UserSelectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/usermanager.do")
public class UserManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String flag = req.getParameter("flag");
        if (flag != null) {
            switch (flag) {
                case "addUser":
                    this.addUser(req, resp);
                    break;
                case "selectUser":
                    this.selectUser(req, resp);
                    break;
                case "preUpdate":
                    this.preUpdate(req, resp);
                    break;
                case "updateUser":
                    this.updateUser(req, resp);
                    break;
                case "deletUser":
                    this.deletUser(req, resp);
                    break;
            }
        }
    }

    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        User user = this.createUser(req);
        try {
            UserLoginService userLoginService = new UserLoginServiceImpl();
            userLoginService.addUser(user);
            resp.sendRedirect("ok.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                resp.sendRedirect("error.jsp");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    public User createUser(HttpServletRequest req) throws UnsupportedEncodingException {
        String username = req.getParameter("username");

        String newUsername = null;
        if (username != null) {
            newUsername = new String(username.getBytes("iso-8859-1"), "utf-8");
        } else {
            newUsername = "";
        }

        String usersex = req.getParameter("usersex");

        String newusersex = null;
        //因为usersex如果为空的话 调用user.getbytes会报错
        if (usersex != null) {
            newusersex = new String(usersex.getBytes("iso-8859-1"), "utf-8");
        } else {
            newusersex = "";
        }

        String userpwd = req.getParameter("userpwd");
        String phoneNum = req.getParameter("phoneNum");
        String qqNum = req.getParameter("qqNum");


        User user = new User();
        user.setUsersex(newusersex);
        user.setUserpwd(userpwd);
        user.setUsername(newUsername);
        user.setQqNum(qqNum);
        user.setPhoneNum(phoneNum);


        return user;
    }

    public void selectUser(HttpServletRequest req, HttpServletResponse resp) {

        try {
            User user = this.createUser(req);
            UserSelectService userSelectService = new UserSelectServiceImpl();
            List<User> list = userSelectService.selectUser(user);
            req.setAttribute("list", list);
            try {
                req.getRequestDispatcher("view.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            try {
                req.getRequestDispatcher("error.jsp").forward(req, resp);
            } catch (ServletException servletException) {
                servletException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }

    public void preUpdate(HttpServletRequest req, HttpServletResponse resp) {
        String userid = req.getParameter("userid");
        try {
            UserSelectService userSelectService = new UserSelectServiceImpl();
            User user = userSelectService.selectUserById(Integer.parseInt(userid));
            req.setAttribute("user1", user);
            req.getRequestDispatcher("userUpdate.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                req.getRequestDispatcher("error.jsp").forward(req, resp);
            } catch (ServletException servletException) {
                servletException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    public void updateUser(HttpServletRequest req, HttpServletResponse resp) {

        String userid = req.getParameter("userid");
        try {
            User user = this.createUser(req);
            user.setUserid(Integer.parseInt(userid));

            UpdateUserService updateUserService = new UpdateUserServiceImpl();
            updateUserService.updateUser(user);

            resp.sendRedirect("ok.jsp");

        } catch (Exception e) {
            e.printStackTrace();
            try {
                resp.sendRedirect("error.jsp");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    public void deletUser(HttpServletRequest req, HttpServletResponse resp) {
        String userid = req.getParameter("userid");

        try {
            DeletUserService deletUserService = new DeletUserServiceImpl();
            deletUserService.deletUser(Integer.parseInt(userid));

            resp.sendRedirect("ok.jsp");
        } catch (IOException e) {
            e.printStackTrace();
            try {
                resp.sendRedirect("error.jsp");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }

}
